package com.hsy.crm.web.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

@SuppressWarnings("ALL")
public class FileCompressUtils {

    private static final String FILE_PATH = "D:\\wuwen\\test\\crm.7z";
    private static final String FILE_PATH_PASS = "D:\\wuwen\\test\\crm.ww";

    //
    public static void main(String[] args) throws Exception {
        jiami();
        //jiemi();
    }

    public static void jiemi() throws Exception {
        File file = new File(FILE_PATH);
        File passFile = new File(FILE_PATH_PASS);

        if( file.exists() ){
            file.delete();
        }

        FileInputStream fis = new FileInputStream(passFile);
        FileOutputStream fos = new FileOutputStream(file);

        byte[] data = new byte[4];

        int readLength;

        while( (readLength = fis.read(data)) != -1 ){

            int dataLength = byte2int(data);

            byte[] dataBody = new byte[dataLength];

            fis.read(dataBody);

            byte[] decryptData = decrypt(dataBody);

            fos.write(decryptData);

        }

        fos.flush();
        fos.close();
        fis.close();
    }

    public static void jiami() throws Exception {
        File file = new File(FILE_PATH);
        File passFile = new File(FILE_PATH_PASS);

        if( passFile.exists() ){
            passFile.delete();
        }

        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(passFile);

        byte[] data = new byte[1024];

        int readLength;

        while( (readLength = fis.read(data)) != -1 ){
            byte[] newData = Arrays.copyOfRange(data,0,readLength);
            byte[] encryptData = encrypt(newData);

            byte[] lengthData = int2byte(encryptData.length);

            fos.write(lengthData);
            fos.write(encryptData);

        }

        fos.flush();
        fos.close();
        fis.close();
    }

    public static int byte2int(byte[] bytes) {
        return (bytes[0]&0xff)<<24
                | (bytes[1]&0xff)<<16
                | (bytes[2]&0xff)<<8
                | (bytes[3]&0xff);
    }

    public static byte[] int2byte(int num){
        byte[]bytes=new byte[4];
        bytes[0]=(byte) ((num>>24)&0xff);
        bytes[1]=(byte) ((num>>16)&0xff);
        bytes[2]=(byte) ((num>>8)&0xff);
        bytes[3]=(byte) (num&0xff);
        return bytes;
    }

    //必须为16位
    private static final String KEY = "xos90w!@*ijsld51";

    private static byte[] KEY_BYTES;

    static {
        try {
            KEY_BYTES = KEY.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    // 加密
    public static byte[] encrypt(byte[] bytes){
        SecretKeySpec skeySpec = new SecretKeySpec(KEY_BYTES, "AES");
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(bytes);
            return encrypted;
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    // 解密
    public static byte[] decrypt(byte[] bytes){
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(KEY_BYTES, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] original = cipher.doFinal(bytes);
            return original;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
